/*
 * This file is part of org.kalmeo.apps.kaprice.
 * 
 * org.kalmeo.apps.kaprice is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * org.kalmeo.apps.kaprice is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with org.kalmeo.apps.kaprice.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 12 mars 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 */

package com.ilove.weather.frame;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.util.frame.Frame;

import com.ilove.core.ILOVE;

import com.ilove.weather.*;

/**
 * @author omarino
 */
public class MainFrame implements Frame {

	static int posSelected = 0; // Province selected

	// static instance of MainFrame
	public static MainFrame instance = new MainFrame();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (ILOVE.screenMain != null) {
			ILOVE.screenMain.setCurrent();
		}
	}

	/**
	 * @param direction
	 *            1 = up|down 2 = left 3 = right
	 */
	private void changePosition(int direction) {
		int preIdSelected = posSelected;
		if (direction == 1) { // up | down
			switch (posSelected) {
			case 0:
				posSelected = 3;
				break;
			case 1:
				posSelected = 4;
				break;
			case 2:
				posSelected = 5;
				break;
			case 3:
				posSelected = 0;
				break;
			case 4:
				posSelected = 1;
				break;
			case 5:
				posSelected = 2;
				break;
			}
		} else if (direction == 3) { // right
			posSelected++;
			if (posSelected > 5)
				posSelected = 0;
		} else if (direction == 2) { // left
			posSelected--;
			if (posSelected < 0)
				posSelected = 5;
		}
		selected(preIdSelected, posSelected);

	}

	private void selected(int preIdSelected, int idSelected) {
		Picture picturePreSelected = (Picture) ILOVE.screenMain
				.getWidget(String.valueOf(preIdSelected));
		picturePreSelected.setFrameSequence(new int[] { preIdSelected });
		Picture picture = (Picture) ILOVE.screenMain.getWidget(String
				.valueOf(idSelected));
		picture.setFrameSequence(new int[] { idSelected + 6 });
		ILOVE.screenMain.setTitle(Constant.AREA_NAME[idSelected]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("showMainScreen".equals(identifier)) {
			showScreen();
			return false;
		} else if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and
			// null arguments
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("next".equals(identifier)) {
			ProvinceFrame.instance.areaName = Constant.AREA_NAME[posSelected];
			ProvinceFrame.posSelected = posSelected;
			// Kuix.getFrameHandler().pushFrame(WaitingFrame.instance);
			Kuix.getFrameHandler().pushFrame(ProvinceFrame.instance);
			return false;
		} else if ("actUp".equals(identifier)) {
			changePosition(1);
			return false;
		} else if ("actRight".equals(identifier)) {
			changePosition(3);
			return false;
		} else if ("actDown".equals(identifier)) {
			changePosition(1);
			return false;
		} else if ("actLeft".equals(identifier)) {
			changePosition(2);
			return false;
		} else if ("about".equals(identifier)) {
			Kuix
					.alert("Phần mềm cung cấp thông tin Dự Báo Thời Tiết. Phiên bản 1.0");
			return false;
		} else if ("member".equals(identifier)) {
			String UID = RMSManager.readUID();
			Kuix.alert("Tài khoản của bạn: "
					+ (UID == null ? "Chưa đăng ký" : UID));
			return false;
		} else if ("sendsms".equals(identifier)) {
			System.out.println(ILOVE.message + " ==> " + ILOVE.destination);
			new SMSender(ILOVE.message, ILOVE.destination).send();
			return false;
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onAdded()
	 */
	public void onAdded() {
		try {
			if (ILOVE.UID == null) ILOVE.UID = RMSManager.readUID();
			ILOVE.screenMain = Kuix.loadScreen(Constant.SCREEN_AREA, null);
			ILOVE.screenMain.setCurrent();
			selected(0, 0);
		} catch (Exception e) {
			Kuix.alert("Không load được dữ liệu. Vui lòng thử lại sau.");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onRemoved()
	 */
	public void onRemoved() {
		System.gc();
		try {
			com.ilove.frame.MainFrame.instance.showScreen();
			ILOVE.screenMain.cleanUp();
			ILOVE.screenMain = null;
		} catch (Exception e) {
		}
	}

}
